iT邦幫忙

DAY 39
5

[鐵鷹練成]CodeIgniter + Bootstrap系列 第 31

[鐵鷹練成]文章區(文章分類管理-end)[3]

  • 分享至 

  • xImage
  •  

今天又遲疑了...
聽某人推薦AngularJS....
我對JQ又有遲疑了(雖然可以共用)
今天把文章分類管理END掉好了
新增 搜尋 刪除 之功能
還有文章區下拉選單出現新增文章分類
那就來看CODE吧
controllers/hung.php

<?php
class hung extends CI_Controller{
    
    function __construct() {
        parent::__construct();
        $this->load->model('sql');
        $this->load->library('session');
        $this->load->helper('url');
         //每個頁面都會有選單
        $this->meun();
    }
    
    /**************************頁面*************************************/
    //該載入的都丟這裡
    function meun(){
        /**site_url(); base_url();請參考 : http://www.codeigniter.org.tw/user_guide/libraries/uri.html **/
        $data['index'] = site_url();
        $data['link'] = base_url();
        $data['file'] = "hung/";
        $data['class_meun'] = $this->sql->all('class');
        $this->load->view('hung/meun',$data);
    }
  
    function admin(){
        $data['query'] = $this->sql->all('class');
        $this->load->view('hung/admin',$data);
    }
    
    function like_class(){
        $like = $this->input->post();
        $data['query'] = $this->sql->like($like,'class');
        $this->load->view('hung/admin',$data);
    }


    /************************新增***********************/
    
    function AddClass(){
        $all = $this->input->post();
        $this->sql->add($all,'class');
        redirect(site_url().'hung/admin');
    } 
    
    /************************刪除**********************/
    
    function DelClass($id){
        $array = array('class_id'=>$id);
        $this->sql->where_del($array,'class');
        redirect(site_url().'hung/admin');
    }
    
}
?>

以下是我整理的超懶人包,常用的db function,需要長串的就自KEY吧!!
models/sql.php

<?php
class sql extends CI_Model{
    
    function __construct() {
        parent::__construct();
        $this->load->database();
    }
   /****************提取*****************/
    function all($table){
        $query = $this->db->get($table);
        return $query;
    }
   
    function where_array($array,$table){
        $query = $this->db->where($array)->get($table);
        return $query;
    }
           
    function like($array,$table){
        $query = $this->db->like($array)->get($table);
        return $query;
    }
   
    function where_like($array,$title,$like,$table){
        $query = $this->db->where($array)->like($title,$like)->get($table);
        return $query;
    }
           
    function array_in($title,$array,$table){
        $query = $this->db->where_in($title,$array)->get($table);
        return $query;
    }
  
    function select($sql,$tbale){
        $this->db->select($sql);
        $query = $this->db->get($tbale);
        return $query;
    }
  
    function big($title,$table){
        $query = $this->db->select_max($title)->get($table);
        return $query;
    }
  
    function small($title,$table){
        $query = $this->db->select_min($title)->get($table);
        return $query;
    }
  
    function avg($title,$tbale){
        $query = $this->db->select_avg($title)->get($tbale);
        return $query;
    }
  
    function sum($title,$table){
        $query = $this->db->select_sum($title)->get($table);
        return $query;
    }
  
    function join($title,$title2,$sql,$table,$table2){
        $this->db->select($title);
        $this->db->from($table);
        $this->db->join($title2,$sql);
        $query = $this->db->get($table2);
        return $query;
    }

    function row($table){
        $query = $this->db->count_all($table);
        return $query;
    }
  
    function where_row($array,$table){
        $query = $this->db->where($array)->count_all_results($table);
        return $query;
    }

    function table_id($table){
        $query = $this->db->insert_id($table);
        return $query;
    }
  
    function where_like_get($where,$title,$like,$table,$num,$offset){
        $query = $this->db->where($where)->like($title,$like)->get($table,$num,$offset);
        return $query;
    }
   
    function array_pag($array,$table,$num,$total){
        $query = $this->db->where($array)->get($table,$num,$total);
        return $query;
    }
   
    function pag($table,$num,$total){
        $query = $this->db->get($table,$num,$total);
        return $query;
    }
           
    function where_order_limit($where,$title,$order,$table,$num){
        $query = $this->db->where($where)->order_by($title,$order)->get($table,$num);
        return $query;
    }
   
    function where_sum($array,$title,$name,$table){
        $query = $this->db->where($array)->select_sum($title,$name)->get($table);
        return $query;
    }


    /******************新增****************/
   
    function add($all,$table){
        $this->db->insert($table,$all);
    }
   
    function set_add($array,$table){
        $this->db->set($array)->insert($table);
    }
   
    /*****************刪除***************/
   
    function del($title,$id,$table){
        $this->db->where($title,$id)->delete($table);
    }
   
    function where_del($array,$table){
        $this->db->where($array)->delete($table);
    }
   
    /****************修改****************/
   
    function modify($array,$all,$table){
        $this->db->where($array)->update($table,$all);
    }
   
    function set_modify($where,$array,$table){
        $this->db->where($where)->set($array)->update($table);
    }
   
    function all_modify($array,$table){
        $this->db->set($array)->update($table);
    }
    
}
?>

在來是頁面
views/admin.php

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理</title>

<div class="container">
  <table width="1200" border="1" class="table table-striped table-bordered table-condensed">
    <tr>
      <td id="color"><strong>>>>管理</strong></td>
    </tr>
    <tr>
      <td>
      <div align="center">
      <div class="btn-group">
      <button class="btn" type="button" onClick="window.location='<?=$index;?><?=$file;?>admin'">文章分類管理</button>
      <button class="btn" type="button" onClick="window.location='<?=$index;?><?=$file;?>'">文章管理</button>
      <button class="btn" type="button" onClick="window.location='<?=$index;?><?=$file;?>'">使用者管理</button>
      <button class="btn" type="button" onClick="window.location='<?=$index;?><?=$file;?>'">推薦管理</button>
      <button class="btn" type="button" onClick="window.location='<?=$index;?><?=$file;?>'">留言板管理</button>
      </div>
      </div>
      </td>
    </tr>
    <tr>
      <td><table width="1200" border="1" class="table table-striped table-bordered table-condensed table-hover">
        <tr>
          <td colspan="4" id="color2"><strong>>>>文章分類管理</strong></td>
          </tr>
        <tr>
          <td colspan="4"><form id="form1" name="form1" method="post" action="<?=$index;?><?=$file;?>like_class" class="form-inline">
          <div id="heig">
            <strong>
            搜尋:
            <label>
              <input type="text" name="class_name" placeholder="請輸入名稱" />
            </label>
            <label>
              <input type="submit" value="搜尋" class="btn" />
            </label>
            </strong>
            </div>
          </form></td>
          </tr>
        <tr>
          <td colspan="4"><form id="form2" name="form2" method="post" action="<?=$index;?><?=$file;?>AddClass" class="form-inline">
            <div id="heig">
            <strong>
          新增分類-- 名稱:
              <label>
                <input type="text" name="class_name" id="class_name" />
              </label>
            英文
            <label>
              <input type="text" name="class_en" id="class_en" />
            </label>
            <label>
              <input type="submit" value="新增" class="btn" />
            </label>
            </strong>
            </div>
          </form></td>
          </tr>
        <tr>
          <td id="color3"><div align="center"><strong>項次</strong></div></td>
          <td id="color3"><div align="center"><strong>名稱</strong></div></td>
          <td id="color3"><div align="center"><strong>英文</strong></div></td>
          <td id="color3"><div align="center"><strong>刪除</strong></div></td>
        </tr>
        <?php foreach($query->result_array() as $key=>$row):?>
        <tr>
          <td><div align="center"><strong>
            <?=$key+1;?>
          </strong></div></td>
          <td><div align="center"><strong>
            <?=$row['class_name'];?>
          </strong></div></td>
          <td><div align="center"><strong>
            <?=$row['class_en'];?>
          </strong></div></td>
          <td><div align="center"><a href="<?=$index;?><?=$file;?>DelClass/<?=$row['class_id'];?>">
            <button class="btn btn-mini" type="button">刪除</button>
          </a></div></td>
        </tr>
        <?php endforeach;?>
      </table></td>
    </tr>
  </table>
</div>

然後是改過的meun.php

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>蒼天</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?=$link;?>css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="<?=$link;?>css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="<?=$link;?>js/jquery.js"></script>
<script src="<?=$link;?>js/bootstrap.min.js"></script>
<style>
#color{
     background-color: #666;
     color: #FFF;
}
#color2{
     background-color: #999;
}
#color3{
     background-color: #CCC;
}
#heig{
     height:10px;
}
</style>


<div class="container">
<div id="navbar-example" class="navbar navbar-static">
<div class="navbar-inner">
<div class="container" style="width: auto;">
<a class="brand" href="#"><strong>蒼天</strong></a>
<ul class="nav" role="navigation">
<li class="dropdown">
<a id="drop1" href="#" role="button" class="dropdown-toggle" data-toggle="dropdown"><strong>介紹</strong></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>網站介紹</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>站長介紹</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>聯絡站長</strong></a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" id="drop2" role="button" class="dropdown-toggle" data-toggle="dropdown"><strong>文章區</strong></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop2">
<!--這邊以後會從資料庫的文章類別來foreach-->
<?php foreach($class_meun->result_array() as $row):?>
<li role="presentation"><a role="menuitem" tabindex="-1" href="<?=$index;?><?=$file;?>article_class/<?=$row['class_en'];?>"><?=$row['class_name'];?></a></li>
<?php endforeach; ?>
</ul>
</li>
<li class="dropdown">
<a href="#" id="drop3" role="button" class="dropdown-toggle" data-toggle="dropdown"><strong>其他功能</strong></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop3">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>留言板</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>討論區</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>相簿區</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>暫留</strong></a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" id="drop3" role="button" class="dropdown-toggle" data-toggle="dropdown"><strong>站長推薦</strong></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop4">
<!--到時候也會有資料庫來管理友站-->
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>**</strong></a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" id="drop3" role="button" class="dropdown-toggle" data-toggle="dropdown"><strong>管理</strong></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop5">
<!--到時後登入時只會看到個人資料管理 沒有登入時會出現登入 網站管理也是一樣 做判斷 -->
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>登入</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#"><strong>個人資料管理</strong></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="<?=$index;?><?=$file;?>admin"><strong>網站管理</strong></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>

成功畫面

有空在大爆發(大爆肝)


上一篇
[鐵鷹練成]文章區(文章分類管理-view)[2]
系列文
[鐵鷹練成]CodeIgniter + Bootstrap31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
卡斯
iT邦研究生 1 級 ‧ 2013-10-25 23:35:10

沙發

謝謝

0

拍手拍手

謝謝

我要留言

立即登入留言